﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Data;
using MyPublisher.Administration.Commands;
using System.Windows.ApplicationFramework;
using System.Collections.ObjectModel;
using MyPublisher.Administration.Model;
using MyPublisher.Framework;
using MyPublisher.Administration.Views;

namespace MyPublisher.Administration.ViewModels
{
    public class PackageViewModel : DocumentViewModel<PackageDocument>
    {
        private ICommand publishCommand;

        public PackageViewModel(IView view, string name)
            : base(view, name)
        {
            this.InsertView(new PackageDetailViewModel(new PackageDetailView(), "Detail"));
            this.InsertView(new PackageContentsViewModel(new PackageContentsView(), "Documents"));
            this.SelectedView = this.Views[0];

            this.InsertItems(App.EntityFactory.Packages);
            this.SelectFirst();
        }

        public ICommand PublishCommand
        {
            get
            {
                if (publishCommand == null)
                {
                    publishCommand = new DelegateCommand(Publish,CanPublish);
                }
                return publishCommand;
            }
        }

        private void Publish()
        {
            PackageSession session = new PackageSession(App.EntityFactory, SelectedItem as Package);
            session.Publish(
                new ArchivePublishProvider(App.EntityFactory.Locations[1])
                );
        }

        private bool CanPublish() { return SelectedItem != null; }
    }
}
